package com.chinapost.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.excelUtil.ExcelUtils;
import com.chinapost.excelUtil.JsGridReportBase;
import com.chinapost.excelUtil.TableData;
import com.chinapost.service.DeptServiceI;
import com.chinapost.service.TqsjServiceI;
import com.chinapost.service.UserServiceI;
import com.chinapost.vo.DataGrid;
import com.chinapost.vo.Json;
import com.chinapost.vo.SessionInfo;
import com.chinapost.vo.Tqsj;
import com.chinapost.vo.YxsjTj;
import com.opensymphony.xwork2.ModelDriven;

@Action(value = "tqsjAction", results = { 
		@Result(name = "tqsjManager", location = "/jsp/tqsj/tqsjManager.jsp"),
		@Result(name = "manager", location = "/jsp/tqsj/manager.jsp"),
		@Result(name = "tqsjTj", location = "/jsp/tj/tqsjTj.jsp"),
		@Result(name = "tqsjTjone", location = "/jsp/tj/tqsjTjone.jsp")
		})
public class TqsjAction extends BaseAction implements ModelDriven<Tqsj>, ServletRequestAware,ServletResponseAware {

	private Tqsj tqsj = new Tqsj();
	private HttpServletRequest request;
	private HttpServletResponse response;
	@Autowired
	private TqsjServiceI tqsjService;
	@Autowired
	private DeptServiceI deptService;
	@Autowired
	private UserServiceI userService;

	@Override
	public Tqsj getModel() {
		return tqsj;
	}

	public String tqsjManager() {
		SessionInfo si=(SessionInfo)ServletActionContext.getRequest().getSession().getAttribute("sessionInfo");
		String roleIds = si.getRoleIds();
		if (roleIds.contains("9999")){//普通用户的角色ID是9999
			return "manager";//普通用户返回manager.jsp页面
		}else{
			return "tqsjManager";
		}
	}

	public String tqsjTj(){
		SessionInfo si=(SessionInfo)ServletActionContext.getRequest().getSession().getAttribute("sessionInfo");
		String roleIds = si.getRoleIds();
		if (roleIds.contains("9999")){//普通用户的角色ID是9999
			return "tqsjTjone";//普通用户返回manager.jsp页面
		}else{
			return "tqsjTj";
		}
	}
	
	public void datagrid() {
		if (!StringUtils.isEmpty(tqsj.getDeptid())) {
			tqsj.setDeptIds(deptService.getChildIds(tqsj.getDeptid()));
		} else {
			SessionInfo s = (SessionInfo) request.getSession().getAttribute("sessionInfo");
			tqsj.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}

		super.writeJsonDate(tqsjService.datagrid(tqsj));
	}

	public void addTqsjs(){
		Json j = new Json();
		int count = 0;
		if (!StringUtils.isEmpty(tqsj.getDeptid())) {
			tqsj.setDeptIds(deptService.getChildIds(tqsj.getDeptid()));
		} else {
			SessionInfo s = (SessionInfo) request.getSession().getAttribute("sessionInfo");
			tqsj.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}
		try {
			count = tqsjService.addTqsjs(tqsj);
			if (count > 0) {
				j.setSuccess(true);
				j.setMsg("计算成功，新增" + count + "条提前支取数据！");
			} else {
				j.setMsg("新增0条提前支取数据！");
			}
		} catch (Exception e) {
			j.setMsg("计算提前支取数据失败！");
		}
		super.writeJson(j);
		
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	
	public void exportExcel() throws Exception {
		response.setContentType("application/msexcel;charset=GBK");
		SessionInfo s = (SessionInfo) request.getSession().getAttribute("sessionInfo");
		if (!StringUtils.isEmpty(tqsj.getDeptid())) {
			tqsj.setDeptIds(deptService.getChildIds(tqsj.getDeptid()));
		} else {
			tqsj.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}
		List<Tqsj> list = tqsjService.excelData(tqsj);
		String title = "";
		if (!StringUtils.isEmpty(tqsj.getCbh())){
			title = userService.getNameByBh(tqsj.getCbh());
		}else{
			if (!StringUtils.isEmpty(tqsj.getDeptid())){
				title = deptService.getNameById(tqsj.getDeptid());
			}else{
				title = s.getDeptName();
			}
		}
		title += "提前支取数据表("+tqsj.getStartMonth()+"-"+tqsj.getEndMonth()+")";
		String[] hearders = new String[] { "部门", "编号","姓名", "上报月份", "账号","卡号(一本通)", "业务种类",  "存期", "原余额", "提前支取日期","提前支取金额","应扣营销奖励","备注" };// 表头数组
		String[] fields = new String[] { "deptname", "cbh", "username", "csbyf", "czh","ckh", "kindname", "ccq", "cye", "ctqrq","ctqje","ctqfy","czt" };// 对象属性数组
		int[] columnwidths = new int[] { 18, 10, 10, 12, 16,18, 12, 10, 16,16, 18,16,20 };
		TableData td = ExcelUtils.createTableData(list,ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, "admin", columnwidths, td);
	}
	

	public void tjgr_datagrid(){
		DataGrid dg = new DataGrid();
		if (!StringUtils.isEmpty(tqsj.getDeptid())) {
			tqsj.setDeptIds(deptService.getAllChildids(tqsj.getDeptid()));
		} else {
			SessionInfo s = (SessionInfo) request.getSession().getAttribute("sessionInfo");
			tqsj.setDeptIds(deptService.getAllChildids(s.getDeptid()));
		}
		if (tqsj.getStartMonth()!=null && tqsj.getEndMonth()!=null){
			dg = tqsjService.datagrid_tjgr(tqsj);
		}

		super.writeJsonDate(dg);
	}
	
	public void exportExcel_tjgr() throws Exception {
		response.setContentType("application/msexcel;charset=GBK");
		SessionInfo s = (SessionInfo) request.getSession().getAttribute("sessionInfo");
		if (!StringUtils.isEmpty(tqsj.getDeptid())) {
			tqsj.setDeptIds(deptService.getChildIds(tqsj.getDeptid()));
		} else {
			tqsj.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}
		List<YxsjTj> list = new ArrayList<YxsjTj>();
		if (tqsj.getStartMonth()!=null && tqsj.getEndMonth()!=null){
			list = tqsjService.excelData_tjgr(tqsj);
		}
		String title = "";
		if (!StringUtils.isEmpty(tqsj.getCbh())){
			title = userService.getNameByBh(tqsj.getCbh());
		}else{
			if (!StringUtils.isEmpty(tqsj.getDeptid())){
				title = deptService.getNameById(tqsj.getDeptid());
			}else{
				title = s.getDeptName();
			}
		}
		title += "提前支取数据统计表("+tqsj.getStartMonth()+"-"+tqsj.getEndMonth()+")";
		String[] hearders = new String[] { "部门", "编号","姓名", "提前支取总积分", "应扣奖励" };// 表头数组
		String[] fields = new String[] { "deptname", "cbh", "username", "czjf", "cjh" };// 对象属性数组
		int[] columnwidths = new int[] { 22, 16, 16, 24, 22 };
		TableData td = ExcelUtils.createTableData(list, ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, "admin", columnwidths, td);
	}
	

}
